/*
 * Gedigarr - Falling block game.
 * Copyright (C) 2010  BoredCorp
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
package gedigarr.objects;

import java.awt.Color;
import putil.Putil;

/**
 *
 * @author JC
 */
public abstract class Tetromino {

  /**
   * 3-dimensional array of Cells. An array of 2D arrays specifying the
   * tetromino active cells.
   */
  Cell[][][] grid;
  /**
   * X (width) padding for fine-tuned positioning of tetros on grids.
   */
  int[] xPad;
  /**
   * Y (height) padding for fine-tuned positioning of tetros on grids.
   */
  int[] yPad;
  int state = 0;

  protected void init( boolean[][][] bGrid, int[] xPad, int[] yPad, Color colour ) {
    this.xPad = xPad;
    this.yPad = yPad;
    int frames = bGrid.length;
    int height;
    int width;
    Cell[][][] tempGrid = new Cell[ frames ][][];

    for ( int f = 0; f < frames; f++ ) {
      height = bGrid[f].length;
      tempGrid[f] = new Cell[ height ][];
      for ( int y = 0; y < height; y++ ) {
        width = bGrid[f][0].length;
        tempGrid[f][y] = new Cell[ width ];
        for ( int x = 0; x < width; x++ ) {
          tempGrid[f][y][x] = new Cell( bGrid[f][y][x], bGrid[f][y][x], colour, "images/trans2.png" );
        }
      }
    }
    init( tempGrid );
  }

  protected void init( Cell[][][] grid ) {
    this.grid = grid;
  }

  public void rotateLeft() {
    this.state = addIndex( -1 );
  // this.state = state - 1 < 0 ? frames - 1 : state - 1 % frames;
  }

  public void rotateRight() {
    this.state = addIndex( +1 );
  //  this.state = (state+1) % frames;
  }

  private int addIndex( int direction ) {
    return Putil.wrapIndex( getFrames(), state + direction );
  }
  /*
  private int addIndex( int direction ) {
  int index = state;
  if ( direction == 0 ) {
  return index;
  } else if ( direction > 0 ) { //move right
  index = (index + direction) % frames;
  } else if ( direction < 0 ) { //move left
  direction = Math.abs(direction) % frames;
  index = index - direction < 0 ? frames - direction : index - direction;
  }
  return index;
  }
   */

  public Cell[][] getIndex( int index ) {
    return grid[Putil.wrapIndex( getFrames(), index )];
  }

  public Cell[][] getCurrent() {
    return grid[state];
  }

  public Cell get( int x, int y ) {
    return getCurrent()[y][x];
  }

  public Cell get( int frame, int x, int y ) {
    return getIndex( frame )[y][x];
  }

  public int getXPad() {
    return getXPad( this.state );
  }

  public int getYPad() {
    return getYPad( this.state );
  }

  public int getXPad( int frame ) {
    frame = Putil.wrapIndex( this.getFrames(), frame );
    return xPad[frame];
  }

  public int getYPad( int frame ) {
    frame = Putil.wrapIndex( this.getFrames(), frame );
    return yPad[frame];
  }

  public int getState() {
    return this.state;
  }

  public void setState( int state ) {
    this.state = state;
  }

  public int getFrames() {
    return grid.length;
  }

  public int getHeight() {
    return getHeight( this.state );
  }

  public int getWidth() {
    return getWidth( this.state );
  }

  public int getHeight( int state ) {
    state = Putil.wrapIndex( getFrames(), state );
    return grid[state] == null ? 0 : grid[state].length;
  }

  public int getWidth( int state ) {
    state = Putil.wrapIndex( getFrames(), state );
    return getHeight() == 0 ? 0 : grid[state][0] == null ? 0 : grid[state][0].length;
  }
}
